<?php

namespace app\api\controller;

use app\BaseController;
use app\common\model\AccountModel;
use think\facade\Db;

class Login extends BaseController
{
    /**
     * @var  AccountModel
     */
    private $model;

    public function initialize()
    {
        $this->model = new AccountModel();
    }


    public function codeLogin()
    {
        $result = [
            "status" => "no",
            "data" => [],
            "msg" => "查找失败"
        ];
        $mobile = $this->request->param("mobile", "");

        if (empty($mobile)) {
            $result["msg"] = "请输入手机号";
            return json($result);
        }
        if (!isMobileNumber($mobile)) {
            $result['msg'] = '请输入正确的手机号码';
            return json($result);
        }

        //查找account
        $account = Db::table("gy_account")->where('account_name', $mobile)->find();
        if (empty($account)) {
            $result["msg"] = "手机号未注册";
            return json($result);
        }

        $token = $this->model->getToken($account['account_name'], $account['account_pwd']);
        $time = date('Y-m-d H:i:s');
        $ip = $this->request->ip();

        $loginData = [
            'token' => $token,
            "last_login" => $time,
            "last_ip" => $ip
        ];

        $loginResult = Db::table("gy_account")
            ->where('account_id', $account['account_id'])
            ->update($loginData);

        if ($loginResult) {
            $result['status'] = 'ok';
            $result['msg'] = '登录成功';
            $result['data'] = [
                'uid' => $account['account_id'],
                'user' => $account['account_name'],
                'nick' => $account['nick_name'],
                'limits' => $account['limits'],
                'type' => $account['type'],
                'token' => $token,
                'expire' => strtotime($time) + 60 * 60 * 24 * 7
            ];
        }
        return json($result);
    }

    public function reg()
    {
        $result = [
            "status" => "no",
            "data" => [],
            "msg" => "查找失败"
        ];
        $mobile = $this->request->param("mobile", "");

        if (empty($mobile)) {
            $result["msg"] = "请输入手机号";
            return json($result);
        }
        if (!isMobileNumber($mobile)) {
            $result['msg'] = '请输入正确的手机号码';
            return json($result);
        }

        $type = $this->request->param("type", "1");
        //查找account
        $account = Db::table("gy_account")
            ->where("type", $type)
            ->where('account_name', $mobile)->find();
        if (!empty($account)) {
            $result["msg"] = "当前手机号已注册";
            return json($result);
        }

        $insertData = [
            'account_name' => $mobile,
            'nick_name' => $mobile,
            'account_pwd' => $this->model->password($mobile),
            'type' => $type
        ];

        $result = $this->model->saveData($insertData);
        if ($result['status'] == "ok") {
            $result['msg'] = "注册成功";
        }
        return json($result);
    }
}
